<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/9
 * Time: 17:03
 */
include '../classes/logincheck.php';
include '../classes/conn.php';
$class_num=$_POST['class_num'];
if(islogin()=="teacher_login") {
    $stu_repeat = 0;

//        没有上传名单
    if (empty($_FILES['name_list']['tmp_name'])) {
        die("file_none");
    } else {
        //        判断上传文件的格式和大小
        if (($_FILES['name_list']['type'] != "application/vnd.ms-excel")
            && ($_FILES['name_list']['type'] != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            echo "type_limit";
            exit();
        } else if ($_FILES['name_list']['size'] > 2 * 1024 * 0124) {
            echo "size_limit";
            exit();
        } //         已经上传文件且格式和大小正确
        else {
            move_uploaded_file($_FILES['name_list']['tmp_name'],
                "../Files/" . $_SESSION['t_num'] . "/namelist/" . $_FILES['name_list']['name']);
            $dir="requests/Files/". $_SESSION['t_num'] . "/namelist/" . $_FILES['name_list']['name'];
            include "../classes/handle_excel.php";
            //            插入学生
            for ($i = 0; $i < count($class_num_array); $i++) {
                $psw = md5(substr($class_num_array[$i], -6));
                $query2 = "insert into student (num,psw,name,class,teacher) values
('{$class_num_array[$i]}','{$psw}','{$class_name_array[$i]}','{$class_num}','{$_SESSION['t_num']}')";

                //            判断学生是否存在
                $query_exist = "select num from student where num='{$class_num_array[$i]}' and class='{$class_num}' and teacher='{$_SESSION['t_num']}'";
                $res = mysql_query($query_exist);
                if ($class_num_array[$i] != null) {
                    if (mysql_num_rows($res)) {
                        $stu_repeat++;
                    } else {
                        mysql_query($query2);
                    }
                }
            }
            if ($stu_repeat > 0) {
                echo "stu_repeat";
            } else {
                echo "1";
            }
            mysql_close($conn);

        }
    }
}

else{
    echo "no_login";
}

